package com.android.takeaway.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.android.takeaway.R;
import com.android.takeaway.Util;
import com.android.takeaway.database.UserDao;
import com.android.takeaway.activity.MainActivity;

/**
 * 登录页面
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btnLogin;
    private EditText etAccount;
    private EditText etPassword;
    private TextView tvRegister;
    private UserDao userDao;
    private CheckBox cbRemember;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sharedPreferences = getSharedPreferences("my_sp", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        initView();
    }

    public void initView() {
        // 去除默认标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) actionBar.hide();
        // 绑定控件
        btnLogin = findViewById(R.id.btn_login);
        tvRegister = findViewById(R.id.tv_register);
        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);
        cbRemember = findViewById(R.id.cb_remember);

        btnLogin.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        // 上次登录时复选框如果被选中，那么自动填充账号和密码且勾选复选框
        boolean isRemember = sharedPreferences.getBoolean("remember", false);
        if (isRemember) {
            cbRemember.setChecked(true);
            etAccount.setText(sharedPreferences.getString("acc", ""));
            etPassword.setText(sharedPreferences.getString("pass", ""));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    // 结果码正确取出传值并显示在文本框中
                    String acc = data.getStringExtra("acc");
                    String pass = data.getStringExtra("pass");
                    etAccount.setText(acc);
                    etPassword.setText(pass);
                }
                break;
            default:
                break;
        }
    }

    private void loginUser() {
        String acc = etAccount.getText().toString().trim();
        String pass = etPassword.getText().toString().trim();
        // 先进行判空
        if (TextUtils.isEmpty(acc) || TextUtils.isEmpty(pass)) {
            Util.showToast(this, "账号或密码不能为空");
            return;
        }
        // 然后判断是否存在该用户
        userDao = new UserDao(getApplicationContext());
        userDao.open();
        if (!userDao.isExist(acc)) {
            Util.showToast(this, "账号不存在");
            userDao.close();
            return;
        }
        // 账号和密码都正确跳转至主页面
        if (userDao.getPassword(acc).equals(pass)) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            // 每次成功登录必须保存登录的账号和密码
            editor.putString("acc", acc);
            editor.putString("pass", pass);
            editor.putBoolean("remember", cbRemember.isChecked());
            editor.commit();
            finish();
        } else {
            Util.showToast(this, "密码错误，请重新输入");
        }
        // 关闭数据库访问对象
        userDao.close();
    }

//    private void registerUser() {
//        // 跳转至注册页面
//        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
//        startActivityForResult(intent, 1);
//    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                loginUser();
                break;
//            case R.id.tv_register:
//                registerUser();
//                break;
        }
    }
}